package main

import "fmt"

func main() {
	fmt.Println(maxProfit([]int{7, 1, 5, 3, 6, 4}))
}

func maxProfit(prices []int) int {
	l := len(prices)
	if l <= 1 {
		return 0
	} else {
		m := make(map[int]int)
		m[0] = prices[0]
		profit := 0
		for i := 1; i < l; i++ {
			m[i] = min(m[i-1], prices[i])
			profit = max(profit, prices[i]-m[i-1])
		}

		if profit < 0 {
			profit = 0
		}

		return profit
	}
}

func min(a, b int) int {
	if a < b {
		return a
	} else {
		return b
	}
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}
